/*
 * 文件名：ParklotNumberServiceImpl.java	 
 * 时     间：下午12:31:47
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz073586.tools.util.Tools;
import com.cz073586.tools.util.UUIDGenerateUtil;
import com.local.common.ParklotNumberConstan;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.ParklotNumberMapper;
import com.local.entity.vo.ParklotVo;
import com.local.model.CarParkInExample;
import com.local.model.ParklotNumber;
import com.local.service.face.ParklotNumberService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.entity.PageInfo;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: ParklotNumberServiceImpl
 * @author Administrator
 * @date 2015年11月6日 下午12:31:47
 */
@Service
public class ParklotNumberServiceImpl extends
		BaseServiceImpl<Object, Object, String> implements ParklotNumberService
{
	@Autowired
	private CarParkInMapper carInMapper;
	
	@Autowired
	private ParklotNumberMapper lotNumberMapper;
	
	@Override
	public ParklotVo getCurrentLotNumber()
	{
		List<ParklotNumber> listNumber = lotNumberMapper
				.queryByPage(new PageInfo<ParklotNumber>());
		
		int carNumber = ParklotNumberConstan.default_number;
		
		if (Tools.isEmpty(listNumber) || listNumber.isEmpty())
		{
			// 插入默认车位数据
			ParklotNumber tmp = new ParklotNumber();
			tmp.setUuid(UUIDGenerateUtil.generate());
			tmp.setNumber(carNumber);
			lotNumberMapper.insertSelective(tmp);
		}
		
		if (!Tools.isEmpty(listNumber) && !listNumber.isEmpty())
		{
			carNumber = listNumber.get(0).getNumber();
		}
		
		// 统计在场车辆
		
		CarParkInExample ex = new CarParkInExample();
		ex.or().andStatusEqualTo(1);
		int exsitNumber = carInMapper.countByCondition(ex);
		
		ParklotVo lotVo = new ParklotVo();
		lotVo.setCarAllLot(carNumber);
		lotVo.setDayIn(exsitNumber);
		
		int freeNumber = carNumber - exsitNumber;
		lotVo.setFreeLot(freeNumber < 0 ? 0 : freeNumber);
		return lotVo;
	}
	
	@Override
	public BaseMapper<Object, Object, String> getMapper()
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Integer saveCurrentLotNumber(int number) throws BaseException
	{
		try
		{
			
			List<ParklotNumber> list = lotNumberMapper
					.queryByPage(new PageInfo());
			
			if (!list.isEmpty())
			{
				ParklotNumber numberInfo = list.get(0);
				numberInfo.setNumber(number);
				
				lotNumberMapper.updateByPrimaryKeySelective(numberInfo);
				return 1;
			}
			
			ParklotNumber numberInfo = new ParklotNumber();
			numberInfo.setNumber(number);
			numberInfo.setUuid(UUIDGenerateUtil.generate());
			lotNumberMapper.insertSelective(numberInfo);
			
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
		}
		return null;
	}

	@Override
	public Integer saveOrUpdateNumber(ParklotNumber pnm) throws BaseException
	{
		// TODO Auto-generated method stub
		List<ParklotNumber> list = lotNumberMapper
				.queryByPage(new PageInfo());
		if (!list.isEmpty())
		{
			ParklotNumber numberInfo = list.get(0);
			numberInfo.setNumber(!Tools.isEmpty(pnm.getNumber())?pnm.getNumber():0);
			numberInfo.setLotNumber(!Tools.isEmpty(pnm.getLotNumber())?pnm.getLotNumber():0);
			
			lotNumberMapper.updateByPrimaryKeySelective(numberInfo);
			return 1;
		}
		
		ParklotNumber numberInfo = new ParklotNumber();
		numberInfo.setNumber(!Tools.isEmpty(pnm.getNumber())?pnm.getNumber():0);
		numberInfo.setLotNumber(!Tools.isEmpty(pnm.getLotNumber())?pnm.getLotNumber():0);
		numberInfo.setUuid(UUIDGenerateUtil.generate());
		lotNumberMapper.insertSelective(numberInfo);
		return null;
	}
	
}
